package com.xueke.crawler.service.fillDocInfo;

import com.xueke.crawler.helper.HttpHelper;
import com.xueke.crawler.model.DocInfo;
import com.xueke.crawler.model.DocRelation;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weibo on 2017/5/16.
 *
 * @author weibo.qin
 * @version 2017/5/16
 */
@Service
public class ParseDocInfo extends BaseFillDocInfo {
    @Override
    public List<DocInfo> queryDocInfo(List<DocRelation> list) {

        List<DocInfo> returnList = new ArrayList<DocInfo>();

        for (DocRelation docRelation : list) {
            String url = "http://www.zxxk.com/soft/" + docRelation.getDocId() + ".html";
            Document doc = HttpHelper.getSimpleDocument(url);

            String auditUser = doc.select(".review").text().replace("审核人：", "").trim();
            String docName = doc.select("h1.res-title").text();
            String fileAfterName = doc.select("h1.res-title").attr("class").replace("res-title", "").trim();
            String pathName = doc.select(".res-link").text();
            String description = doc.select(".intro-text").text();
            int docClass = docRelation.getDocId();
            String docType = doc.select(".type").text().replace("分类：", "").trim();
            String level = String.valueOf(docRelation.getLevel());
            String fileSize = doc.select(".size.des").text();
            String dateTime = doc.select(".time.des").text();
            String infoArea = doc.select(".address.des").text();
            String jifen = doc.select(".download :eq(0)").text().trim();
            String uperUser = doc.select(".introduce .txt").text().trim();
            String downTimes = doc.select(".down.des").text().trim();
            String back1 = String.valueOf(docRelation.getLeafId());

            DocInfo docInfo = new DocInfo();

            docInfo.setAuditUser(auditUser);
            docInfo.setBack1(back1);
            docInfo.setDateTime(dateTime);
            docInfo.setDescription(description);
            docInfo.setDocClass(docClass);
            docInfo.setDocType(docType);
            docInfo.setDownLevel(level);
            docInfo.setDocName(docName);
            docInfo.setDownTimes(downTimes);
            docInfo.setFileAfterName(fileAfterName);
            docInfo.setJifen(jifen);
            docInfo.setFileSize(fileSize);
            docInfo.setInfoArea(infoArea);
            docInfo.setPathName(pathName);
            docInfo.setUpUser(uperUser);

            System.out.println(docInfo.toString());
            returnList.add(docInfo);
        }

        return returnList;
    }
}
